package com.iptv;


import com.doman.*;
import com.resultmsg.BaseEnum;
import com.resultmsg.BaseEnums;
import com.resultmsg.Result;
import com.resultmsg.Results;
import com.server.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping(value = "/educloud")
@RestController
public class IptvController {

    @Autowired
    private ParentMapper parentMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherParentMapper teacherParentMapper;
    @Autowired
    private BaseConfigMapper baseConfigMapper;
    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private ComboMapper comboMapper;


    //查询这个用户是否有绑定学生没绑定的话返回绑定页面   有绑定的话返回查询用户手机否付费   不是付费的话去支付   是付费的话点击绑定   是付费已绑定的有IPTV账号的提示再次绑定
    @RequestMapping(value = "/bindtype/{openid}", method = RequestMethod.GET)
    @ResponseBody
    public Result bindtype(@PathVariable("openid") String openid) {
        //查询这个家长有没有绑定过学生
        //查询平台信息
//        TeacherParent teacherParent = new TeacherParent();
//        teacherParent.setOpenid(openid);
//        TeacherParent teacherParent1 = teacherParentMapper.selectByPrimaryKeyOpenid(teacherParent);
        //查询家长信息
        Parent parent = new Parent();
        parent.setOpenid(openid);
        Parent parent1 = parentMapper.selectByPrimaryByOpenid(parent);
        if (parent1 != null){
            //查询学生信息
            Student student = new Student();
            student.setBindcode(parent1.getCurrentcode());
            Student student1 = studentMapper.selectByPrimaryByCurrentCode(student);
            //学生设置班级
            student1.setClassno(student1.getClassno()+"班");
            //判断这个家长有没有绑定着学生
            boolean on = false;
            String[] split = student1.getRoleParents().split(",");
            for (String list : split) {
                if (list.equals(openid)){
                    on = true;
                }
            }
            if (!on){
                return Results.successWithData("未绑定",BaseEnums.SUCCESS.code(), "未绑定");
            }else{
                //查询年级信息
                BaseConfig baseConfig = new BaseConfig();
                baseConfig.setKcode(student1.getGradeno());
                BaseConfig baseConfig1 = baseConfigMapper.selectByPrimaryByKcode(baseConfig);
                //设置年级信息
                student1.setGradeno(baseConfig1.getLabel());
                //查询学校信息
                Tenant tenant = new Tenant();
                tenant.setTenantId(student1.getOrganize());
                Tenant tenant1 = tenantMapper.selectByPrimaryByOrganize(tenant);
                //设置学校名称
                student1.setOrganize(tenant1.getTenantName());
                //设置套餐价格
                Combo combo = new Combo();
                combo.setKcode(tenant1.getOrderType());
                Combo combo1 = comboMapper.selectByPrimaryByKcode(combo);
                student1.setTel(String.valueOf(combo1.getPrice()));
                if (student1.getPayStatus() == 0){
                    return Results.successWithData(student1,BaseEnums.SUCCESS.code(), "未缴费");
                }else{
                    if (student1.getIptvaccount() == null){
                        return Results.successWithData(student1,BaseEnums.SUCCESS.code(),"第一次绑定");
                    }else{
                        return Results.successWithData(student1,BaseEnums.SUCCESS.code(),"再次绑定");
                    }
                }
            }
        }
        return Results.successWithData("",BaseEnums.SUCCESS.code(),"未绑定");


        //
        //未绑定                        绑定页
        //已绑定+已付费+IPTV账号未存在   绑定
        //已绑定+已付费+IPTV账号已存在   弹窗绑定成功
        //已绑定+未付费+IPTV账号未存在   去支付
    }


    //查询这个用户是否有绑定学生没绑定的话返回绑定页面   有绑定的话返回查询用户手机否付费   不是付费的话去支付   是付费的话点击绑定   是付费已绑定的有IPTV账号的提示再次绑定
    @RequestMapping(value = "/bindtype", method = RequestMethod.POST)
    public Result bindtypePost(@RequestBody Student student) {
        int i = studentMapper.updateByBindCode(student);
        if (i > 0){
            return Results.successWithData("",BaseEnums.SUCCESS.code(),"绑定成功");
        }else{
            return Results.successWithData("",BaseEnums.FAILURE.code(),"绑定失败");
        }
    }


}
